package com.ddwl.goods.controller.bargain;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.goods.service.bargain.BargainActivityService;
import com.ddwl.goods.service.bargain.BargainItemService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.bargain.*;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.bargain.BargainActivityDetailVo;
import com.ddwl.schema.vo.goods.bargain.BargainActivityItemVo;
import com.ddwl.schema.vo.goods.bargain.BargainActivityVo;
import com.ddwl.schema.vo.goods.bargain.BargainSkuDetailVo;
import com.ddwl.schema.vo.shop.ShopVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2021/5/25 12:00
 */
@RestController
@RequestMapping("/wbm/boost")
@Api(tags = "五八马助力活动")
public class WbmBoostController extends BaseController {

    @Resource
    private BargainActivityService bargainActivityService;
    @Resource
    private BargainItemService bargainItemService;


    @PostMapping("/add")
    @Auth(product={BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("新增助力活动")
    public RestVo addBoostActivity(@RequestBody @Valid WbmBoostActivityAddBo bo){
        getShopVo(bo.getShopId());
        BargainActivityAddBo addBo = BeanConverUtils.convertBean(bo, BargainActivityAddBo.class);
        bargainActivityService.add(addBo,getProduct());
        return RestVo.ok();
    }

    @PostMapping("/update")
    @Auth(product={BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("修改助力活动")
    public RestVo updateBoostActivity(@RequestBody @Valid WbmBoostActivityUpdateBo bo){
        getShopVo(bo.getShopId());
        BargainActivityUpdateBo updateBo = BeanConverUtils.convertBean(bo, BargainActivityUpdateBo.class);
        bargainActivityService.update(updateBo);
        return RestVo.ok();
    }

    @GetMapping("/{id}")
    @Auth(product={BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE},required = false)
    @ApiOperation("获取助力活动详情")
    public RestVo<BargainActivityDetailVo> getBoostActivityDetail(@PathVariable("id") String id){
        return RestVo.ok(bargainItemService.getDetailById(id));
    }

    @GetMapping("/merchant/{id}")
    @Auth(product={BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("商户端获取助力活动详情")
    public RestVo<BargainActivityDetailVo> getBoostDetailForMerchant(@PathVariable("id") String id,@RequestParam("shopId") String shopId){
        getShopVo(shopId);
        return RestVo.ok(bargainItemService.getDetailById(id));
    }

    @DeleteMapping("/{id}")
    @Auth(product={BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("删除助力活动")
    public RestVo deleteBoostActivity(@PathVariable("id") String id,@RequestParam("shopId") String shopId){
        getShopVo(shopId);
        bargainActivityService.delete(id);
        return RestVo.ok();
    }

    @PostMapping("/page")
    @Auth(product={BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马商户端助力活动分页查询")
    public RestVo<PageVo<BargainActivityVo>> merchantPage(@Valid @RequestBody PageBo<BoostActivityPageBo> pageBo){
        ShopVo vo = getShopVo(pageBo.getParam().getShopId());
        pageBo.getParam().setShopId(vo.getId());
        return RestVo.ok(bargainActivityService.merchantPage(pageBo));
    }

    @PostMapping("/openOrClose")
    @Auth(product={BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("助力活动开启或关闭")
    public RestVo openOrClose(@RequestBody BoostActivityStatusBo bo){
        getShopVo(bo.getShopId());
        BargainActivityStatusBo statusBo = BeanConverUtils.convertBean(bo, BargainActivityStatusBo.class);
        bargainActivityService.openOrClose(statusBo);
        return RestVo.ok();
    }

    @PostMapping("/list")
    @ApiOperation("五八马助力商品列表")
    public RestVo<PageVo<BargainActivityItemVo>> boostList(@Valid @RequestBody PageBo<BoostActivityItemPageBo> pageBo){
        return RestVo.ok(bargainItemService.boostList(pageBo));
    }

    @GetMapping("/getBoostSku")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE}, required = false)
    @ApiOperation("app获取商品sku信息")
    public RestVo<List<BargainSkuDetailVo>> getBoostSku(@RequestParam String activityId, @RequestParam String itemId) {
        return RestVo.ok(bargainItemService.getBargainSku(activityId, itemId));
    }


}
